import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// 报警信息数据结构定义
export interface AlarmInfoField {
  /** 唯一标识 */
  id: number;

  /** 项目ID */
  projectId: number;

  /** 报警名称 */
  alarmName: string;

  /** 报警联系人 */
  alarmContact: string;

  /** 报警级别 */
  alarmLevel: string;

  /** 是否已处理 */
  processed: boolean;

  /** 报警时间 */
  alarmTime: string | Date;

  /** 记录创建时间 */
  createdAt: string | Date;

  /** 所属设备 */
  fromDeviceId: number;

  /** 更新时间 */
  updatedAt: string | Date;

  /** 报警阈值 */
  threshold: string;

  /** 报警内容(英文) */
  contentEn: string;

  /** 报警内容(中文) */
  contentZh: string;

  /** 已读/未读 */
  isRead: boolean;

  /** 告警类型 */
  type: number;
}

export const createAlarmInfoField = (
  tableAction: TableAction<AlarmInfoField> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input-number",
      key: "id",
      title: "ID",
      dataIndex: "id",
      placeholder: "报警记录ID",
      disabled: true,
      width: 100,
      rules: [
        { required: true, message: "ID不能为空" },
        { type: "number", min: 1, message: "ID必须大于0" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "所属项目",
      dataIndex: "projectId",
      key: "projectId",
      placeholder: "请输入项目ID",
      width: 100,

      rules: [
        { required: false },
        {
          type: "number",
          min: 0,
          max: 32767,
          message: "项目ID必须在0-32767范围内",
        },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "报警名称",
      width: 200,
      dataIndex: "alarmName",
      key: "alarmName",
      placeholder: "请输入报警名称",
      rules: [
        { required: false },
        { max: 255, message: "报警名称长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "报警联系人",
      dataIndex: "alarmContact",
      visible: false,
      key: "alarmContact",
      placeholder: "请输入报警联系人",
      rules: [
        { required: false },
        { max: 255, message: "报警联系人长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "报警级别",
      dataIndex: "alarmLevel",
      width: 100,
      key: "alarmLevel",
      placeholder: "请选择报警级别",
      options: [
        { value: "critical", title: "严重" },
        { value: "high", title: "高" },
        { value: "medium", title: "中" },
        { value: "low", title: "低" },
        { value: "info", title: "信息" },
      ],
      rules: [
        { required: false },
        { max: 20, message: "报警级别长度不能超过20字符" },
      ],
    },
    {
      valueType: "string",
      title: "是否已处理",
      dataIndex: "processed",
      key: "processed",
      initialValue: false,
      width: 120,

      rules: [{ required: false }],
    },
    {
      valueType: "string",

      inputType: "date-picker",
      title: "报警时间",
      dataIndex: "alarmTime",
      width: 250,
      key: "alarmTime",
      placeholder: "请选择报警时间",
      rules: [{ required: false }],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "所属设备",
      width: 120,
      dataIndex: "fromDeviceId",
      key: "fromDeviceId",
      placeholder: "请输入设备ID",
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "报警阈值",
      visible: false,
      dataIndex: "threshold",
      key: "threshold",
      placeholder: "请输入报警阈值",
      rules: [
        { required: false },
        { max: 100, message: "报警阈值长度不能超过100字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "报警内容(英文)",
      dataIndex: "contentEn",
      visible: false,
      key: "contentEn",
      placeholder: "请输入英文报警内容",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [
        { required: false },
        { max: 255, message: "英文报警内容长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "报警内容(中文)",
      visible: false,
      dataIndex: "contentZh",
      key: "contentZh",
      placeholder: "请输入中文报警内容",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [
        { required: false },
        { max: 255, message: "中文报警内容长度不能超过255字符" },
      ],
    },
    {
      title: "已读/未读",
      dataIndex: "isRead",
      key: "isRead",
      initialValue: false,
      width: 120,
      rules: [{ required: false }],
    },
    {
      valueType: "number",
      inputType: "select",
      title: "告警类型",
      dataIndex: "type",
      key: "type",
      placeholder: "请选择告警类型",
      options: [
        { value: 1, title: "设备故障" },
        { value: 2, title: "阈值告警" },
        { value: 3, title: "状态异常" },
        { value: 4, title: "通信中断" },
        // 可根据实际情况添加更多选项
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      visible: false,
      dataIndex: "createdAt",
      key: "createdAt",
      disabled: true,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      visible: false,
      disabled: true,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: AlarmInfoField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除此报警记录？"
            description="删除记录将无法恢复"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
